# -*- coding: utf-8 -*-

from time import time, strftime

import wx
import wx.lib.ogl as ogl

import RLine




class EntityShape(ogl.DividedShape):
    def __init__(self, name, width, height, canvas):
        ogl.DividedShape.__init__(self, width, height)
        self.name = name
        self.canvas = canvas
        
        region1 = ogl.ShapeRegion()        
        region1.SetName(self.name)
        region1.SetText(self.name)
        #region1.SetMinSize(10,10)
        region1.SetProportions(0, 0.7)
        region1.SetFormatMode(ogl.FORMAT_CENTRE_HORIZ)
        self.AddRegion(region1)
        
        self.region = region1

        region2 = ogl.ShapeRegion()
        region2.SetName('col')
        region2.SetColour('red')
        region2.SetText(' ')
        #region2.SetMinSize(10,10)
        region2.SetProportions(0, 0.3)
        region2.SetFormatMode(ogl.FORMAT_NONE) #ogl.FORMAT_LEFT_HORIZ|ogl.FORMAT_CENTRE_VERT)
        self.AddRegion(region2)
        
        """
        region3 = ogl.ShapeRegion()
        region3.SetText(' ')
        region3.SetProportions(0, 0.6)
        region3.SetFormatMode(ogl.FORMAT_NONE)
        self.AddRegion(region3)
        """
        
        """
        region4 = ogl.ShapeRegion()
        region4.SetText('Region 4\nwith embedded\nline breaks')
        region4.SetProportions(0.0, 0.9)
        region4.SetFormatMode(ogl.FORMAT_NONE)
        self.AddRegion(region4)
        """
        
        evthandler = MyEvtHandler(canvas.log, canvas.frame)
        evthandler.SetShape(self)
        evthandler.SetPreviousHandler(self.GetEventHandler())
        self.SetEventHandler(evthandler)        
        
        self._shadowBrush = wx.GREY_BRUSH
        self._brush = wx.WHITE_BRUSH
        self.SetShadowMode(ogl.SHADOW_RIGHT)
        
        self.SetRegionSizes()
        self.ReformatRegions(canvas)

    def SetName(self, name):
        self.name = name
        print name
        self.region.SetText(name)
        
    def ReformatRegions(self, canvas=None):
        rnum = 0

        if canvas is None:
            canvas = self.GetCanvas()

        dc = wx.ClientDC(canvas)  # used for measuring

        for region in self.GetRegions():
            text = region.GetText()
            self.FormatText(dc, text, rnum)
            rnum += 1

    def GetPos(self):
        return (self.GetX(), self.GetY())

    def OnSizingEndDragLeft(self, pt, x, y, keys, attch):
        print "***", self
        ogl.DividedShape.OnSizingEndDragLeft(self, pt, x, y, keys, attch)
        self.SetRegionSizes()
        self.ReformatRegions()
        self.GetCanvas().Refresh()


#----------------------------------------------------------------------



class MyEvtHandler(ogl.ShapeEvtHandler):
    def __init__(self, log, frame):
        ogl.ShapeEvtHandler.__init__(self)
        self.log = log
        self.statbarFrame = frame

    def UpdateStatusBar(self, shape):
        x, y = shape.GetX(), shape.GetY()
        width, height = shape.GetBoundingBoxMax()
        self.statbarFrame.SetStatusText("Pos: (%d, %d)  Size: (%d, %d)" %
                                        (x, y, width, height))
                                        
    def OnLeftClick(self, x, y, keys=0, attachment=0):
        print "on left click"
        shape = self.GetShape()
        
        if isinstance(shape, EntityShape):
            #shape.SetWidth(200)
            #shape.SetHeight(200)
            #canvas = shape.canvas
            #line = RLine.RLine(canvas, canvas.fromShape, shape)
            
            #canvas.diagram.AddShape(line)
            #line.Show(True)
            pass
        else:
            print type(shape)
        #shape.SetName(time())
        for region in shape.GetRegions():
            if region.GetName() == 'col':
                #pass
                #region.ClearText()
                #print ">>>>>>>>in col"
                #region.SetName("name\nset")
                region.SetText("name\nset")
            """
            for item in region.GetFormattedText():
                print item.GetText()
                print item.SetText(item.GetText() + '_a')
            print region.GetName()
            print region.GetText()
            print "end region for"
            """
        canvas = shape.GetCanvas()
        
        dc = wx.ClientDC(canvas)
        
        canvas.PrepareDC(dc)
        #Redraw(dc)
        
        if shape.Selected():
            #shape.name=time.time()
            shape.Select(False, dc)
            canvas.Redraw(dc)
        else:
            redraw = False
            shapeList = canvas.GetDiagram().GetShapeList()
            toUnselect = []

            for s in shapeList:
                if s.Selected():
                    # If we unselect it now then some of the objects in
                    # shapeList will become invalid (the control points are
                    # shapes too!) and bad things will happen...
                    toUnselect.append(s)

            shape.Select(True, dc)

            if toUnselect:
                for s in toUnselect:
                    s.Select(False, dc)

                canvas.Redraw(dc)
        #ogl.ShapeEvtHandler.OnLeftClick(self, )
        #self.OnSizingEndDragLeft(0, x, y, keys, attachment)
        #canvas.Refresh()
        self.UpdateStatusBar(shape)



    def OnLeftDoubleClick(self, x, y, keys = 0, attachment = 0):
        print "double click"

    def OnEndDragLeft(self, x, y, keys=0, attachment=0):
        print "on end drag left"
        shape = self.GetShape()
        ogl.ShapeEvtHandler.OnEndDragLeft(self, x, y, keys, attachment)

        if not shape.Selected():
            self.OnLeftClick(x, y, keys, attachment)

        self.UpdateStatusBar(shape)


    def OnSizingEndDragLeft(self, pt, x, y, keys, attch):
        print "On Sizing End Drag Left"
        ogl.ShapeEvtHandler.OnSizingEndDragLeft(self, pt, x, y, keys, attch)
        self.UpdateStatusBar(self.GetShape())


    def OnMovePost(self, dc, x, y, oldX, oldY, display):
        print "on move post"
        ogl.ShapeEvtHandler.OnMovePost(self, dc, x, y, oldX, oldY, display)
        self.UpdateStatusBar(self.GetShape())


    def OnRightClick(self, *dontcare):
        print "on right click"
        #name = "%s" %(time())
        print self.GetShape() #.name = name
        #self.log.WriteText("%s\n" % self.GetShape())
    """
    def  OnDrawControlPoints(self, dc):
        shape = self.GetShape()
        if isinstance(shape, wx.lib.ogl._lines.LineShape):
            
            print "draw is line"
  
    def OnEraseControlPoints(self, dc):        
        shape = self.GetShape()
        if isinstance(shape, wx.lib.ogl._lines.LineShape):
            
            print "erase is line"
    """
#----------------------------------------------------------------------
